package com.scpii.universal.pns.dao.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.scpii.universal.hibernate.dao.impl.BaseDaoImpl;
import com.scpii.universal.pns.dao.CommentsDao;
import com.scpii.universal.pns.entity.Comments;

@Repository
public class CommentsDaoImpl extends BaseDaoImpl<Comments, Long> implements
		CommentsDao {
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> findBySql(final String sql,
			final Map<String, Object> map) {
		List<Comments> lstResult = null;
		lstResult = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql).addEntity("comm",
						Comments.class);
				if (map != null) {
					Set<String> keySet = map.keySet();
					for (String string : keySet) {
						Object obj = map.get(string);
						if (obj instanceof Collection<?>) {
							query.setParameterList(string, (Collection<?>) obj);
						} else if (obj instanceof Object[]) {
							query.setParameterList(string, (Object[]) obj);
						} else {
							query.setParameter(string, obj);
						}
					}
				}
				return query.list();
			}
		});
		return lstResult;
	}
}
